package com.dtjd.playcoinmonkey.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityOptionsCompat;

import com.dtjd.playcoinmonkey.MyApplication;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.databinding.ActivityWelcomeBinding;
import com.dtjd.playcoinmonkey.utils.Network;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class WelcomeActivity extends Activity {
    private ActivityWelcomeBinding binding;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWelcomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        Configuration config = getResources().getConfiguration();
        String language = MyApplication.sp.getString("language", "default");
        if (!language.equals("default")){
            if (language.equals("Chinese")){
                config.locale = Locale.SIMPLIFIED_CHINESE;
            }else {
                config.locale = Locale.ENGLISH;
            }
            getResources().updateConfiguration(config, getResources().getDisplayMetrics());
        }
        MyApplication.needTicket=true;
        List<HttpParaBean> para=new ArrayList<>();
        Network.post(para, "wbhMain/getNeedTicket", new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                runOnUiThread(() -> final_());
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                runOnUiThread(() -> {
                    try {
                        MyApplication.needTicket= new JSONObject(response.body().string()).getBoolean("datalist");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    final_();
                });
            }
        });
    }
    private void final_() {
        startActivity(new Intent(WelcomeActivity.this, SplashAdActivity.class), ActivityOptionsCompat.makeSceneTransitionAnimation(WelcomeActivity.this, binding.getRoot(), "transition").toBundle());
        finish();
    }
}